<template>
    <div style="opacity: 0.88;min-height: 100vh">
      <div class="manage-form">
        <div class="manage-title">
          <h2>用户商品浏览记录</h2>
        </div>
        <el-table :data="paginatedData" style="width: 100%">
          <el-table-column label="序号" prop="no" />
          <el-table-column label="用户名" prop="username" />
          <el-table-column label="时间" prop="time" />
          <el-table-column label="商品名称" prop="itemName" />
        </el-table>
        <el-pagination
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-size="pageSize"
          :total="looks.length"
          layout="total, prev, pager, next, jumper"
        ></el-pagination>
      </div>
    </div>
  </template>
  
  <script setup>
  import { ref, computed, onMounted } from 'vue';
  import axios from 'axios';
  
  const looks = ref([]);
  const currentPage = ref(1);
  const pageSize = ref(10);
  
  const fetchLooks = async () => {
    try {
      const response = await axios.get('http://localhost:8080/api/products/allLooks');
      looks.value = response.data.sort((a, b) => b.id - a.id).map((look, index) => ({
        no: index + 1,
        id: look.id,
        username: look.username,
        time: look.time,
        itemName: look.itemName,
      }));
    } catch (error) {
      console.error('Error fetching looks:', error);
    }
  };
  
  const paginatedData = computed(() => {
    const start = (currentPage.value - 1) * pageSize.value;
    const end = start + pageSize.value;
    return looks.value.slice(start, end);
  });
  
  const handleCurrentChange = (page) => {
    currentPage.value = page;
  };
  
  onMounted(() => {
    fetchLooks();
  });
  </script>
  
  <style scoped>
  .manage-form .manage-title {
    font-size: 2em;
    text-align: center;
    text-shadow: 5px 5px 5px #0e42ff;
  }
  
  .manage-form {
    background-color: aliceblue;
  }
  
  .el-pagination {
    margin-top: 20px;
    text-align: center;
  }
  </style>
  